//
//  JMLayoutView.m
//  NewiOSProject
//
//  Created by li on 2023/9/4.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "LayoutView.h"
@interface LayoutView()

@property(nonatomic, strong) UIStackView *stackView_H;
@property(nonatomic, strong) UIStackView *stackView_V;

@end

@implementation LayoutView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        [self layoutViews];
    }
    return self;
}

- (void)setupViews {
    
    [self addSubview: self.stackView_H];
    
    [self.stackView_H addArrangedSubview:self.stackView_V];
    [self.stackView_V addArrangedSubview:self.contentView];
    
    [self insertSubview:self.contentBackgroundView atIndex:0];

}
    

- (void)layoutViews {
    
    self.leftMargin = [self.stackView_H.leftAnchor constraintEqualToAnchor:self.leftAnchor];
    self.rightMargin = [self.stackView_H.rightAnchor constraintEqualToAnchor:self.rightAnchor];
    self.topMargin = [self.stackView_H.topAnchor constraintEqualToAnchor:self.topAnchor];
    self.bottomMargin = [self.stackView_H.bottomAnchor constraintEqualToAnchor:self.bottomAnchor];
    
    [NSLayoutConstraint activateConstraints:@[self.leftMargin, self.rightMargin, self.topMargin, self.bottomMargin]];

}

- (void)addContentViews:(NSArray<UIView *> *)subViews {
    
    for (UIView *subview in subViews) {
        
        [self.contentView addArrangedSubview:subview];
    }
}

- (void)addContentView:(UIView *)subView {
    [self.contentView addArrangedSubview:subView];
}

- (NSSet<UIView *> *)removeAllContentArrangeViews {
    NSMutableSet<UIView *> *set = [NSMutableSet set];
    
    while (self.contentView.arrangedSubviews.count != 0) {
        
        UIView *last = self.contentView.arrangedSubviews.lastObject;
        if (last != nil) {
            [self.contentView removeArrangedSubview:last];
            [last removeFromSuperview];
            [set addObject:last];
        }
    }
    
    return set;
}

-(NSInteger)indexOf:(UIView *)subView {
        
    NSInteger index = -1;
    NSInteger count = -1;
    for (UIView *view in self.contentView.arrangedSubviews) {
        count += 1;
        
        if (view == subView) {
            index = count;
            return index;
        }
    }
    
    return index;
}

- (UIStackView *)stackView_H {
    if (!_stackView_H) {
        self.stackView_H = [UIStackView new];
        _stackView_H.translatesAutoresizingMaskIntoConstraints = NO;
        _stackView_H.axis = UILayoutConstraintAxisHorizontal;
    }
    return _stackView_H;
}

- (UIStackView *)stackView_V {
    if (!_stackView_V) {
        self.stackView_V = [UIStackView new];
        _stackView_V.axis = UILayoutConstraintAxisVertical;
    }
    return _stackView_V;
}

- (UIStackView *)contentView {
    if (!_contentView) {
        self.contentView = [UIStackView new];
        _contentView.axis = UILayoutConstraintAxisHorizontal;
    }
    return _contentView;
}

- (UIImageView *)contentBackgroundView {
    
    if (!_contentBackgroundView) {
        _contentBackgroundView = [UIImageView new];
        _contentBackgroundView.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return _contentBackgroundView;
}

- (void)setLayoutViewAlignment:(LayoutViewAlignment)layoutViewAlignment {
    
    _layoutViewAlignment = layoutViewAlignment;
    
    if (layoutViewAlignment == LayoutViewAlignmentLeft) {
        self.stackView_H.alignment = UIStackViewAlignmentCenter;
        self.stackView_V.alignment = UIStackViewAlignmentLeading;
    }else if (layoutViewAlignment == LayoutViewAlignmentCenter) {
        self.stackView_H.alignment = UIStackViewAlignmentCenter;
        self.stackView_V.alignment = UIStackViewAlignmentCenter;
    }else if (layoutViewAlignment == LayoutViewAlignmentRight) {
        self.stackView_H.alignment = UIStackViewAlignmentCenter;
        self.stackView_V.alignment = UIStackViewAlignmentTrailing;
    }else if (layoutViewAlignment == LayoutViewAlignmentTop) {
        self.stackView_H.alignment = UIStackViewAlignmentTop;
        self.stackView_V.alignment = UIStackViewAlignmentCenter;
    }else if (layoutViewAlignment == LayoutViewAlignmentBottom) {
        self.stackView_H.alignment = UIStackViewAlignmentBottom;
        self.stackView_V.alignment = UIStackViewAlignmentCenter;
    }else if (layoutViewAlignment == LayoutViewAlignmentTopLeft) {
        self.stackView_H.alignment = LayoutViewAlignmentTop;
        self.stackView_V.alignment = UIStackViewAlignmentLeading;
    }else if (layoutViewAlignment == LayoutViewAlignmentTopRight) {
        self.stackView_H.alignment = LayoutViewAlignmentTop;
        self.stackView_V.alignment = UIStackViewAlignmentTrailing;
    }else if (layoutViewAlignment == LayoutViewAlignmentBottomLeft) {
        self.stackView_H.alignment = LayoutViewAlignmentBottom;
        self.stackView_V.alignment = UIStackViewAlignmentLeading;
    }else if (layoutViewAlignment == LayoutViewAlignmentBottomRight) {
        self.stackView_H.alignment = LayoutViewAlignmentBottom;
        self.stackView_V.alignment = UIStackViewAlignmentTrailing;
    }
    
    
}



- (void)setContentViewBackgroundColor:(UIColor *)contentViewBackgroundColor {
    
    _contentViewBackgroundColor = contentViewBackgroundColor;
    
    if(@available(iOS 14.0, *)) {
        self.contentView.backgroundColor = contentViewBackgroundColor;
    }else {
        self.contentBackgroundView.backgroundColor = contentViewBackgroundColor;
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
